package com.ubxtech.core.plugins;

import com.ubxtech.core.dto.RequestMappingDTO;
import com.ubxtech.core.dto.RequestMappingServiceDTO;
import com.ubxtech.core.utils.SpringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Victor.Xiao
 * @since 2023-09-05 14:58
 **/
public interface RequestMappingScanService {

    /**
     * 所有接口信息
     */
    List<RequestMappingDTO> MAPPING_LIST = new ArrayList<>();
    /**
     * 需要授权的url
     */
    Set<String> PERMISSION_URLS = new HashSet<>();
    /**
     * 所有的url
     */
    Set<String> ALL_URLS = new HashSet<>();

    /**
     * 处理所有接口url或者标识为要鉴权的接口
     * @author Victor.Xiao
     * @since 2023-09-05 15:00
     * @param serviceId appName
     * @param list list
    */
    default void handle(String serviceId, List<RequestMappingDTO> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        MAPPING_LIST.addAll(list);
        for (RequestMappingDTO requestMappingDTO : list) {
            ALL_URLS.add(requestMappingDTO.getPath());
            if ("1".equalsIgnoreCase(requestMappingDTO.getPermissionFlag())) {
                PERMISSION_URLS.add(requestMappingDTO.getPath());
            }
        }
        RequestMappingServiceDTO requestMappingServiceDTO = new RequestMappingServiceDTO();
        requestMappingServiceDTO.setServiceId(serviceId);
        requestMappingServiceDTO.setRequestMappingList(list);
        SpringUtils.publishEvent(requestMappingServiceDTO);
        customHandle(requestMappingServiceDTO);
    }

    /**
     * 处理所有接口url或者标识为要鉴权的接口
     * @author Victor.Xiao
     * @since 2023-09-05 15:00
     * @param requestMappingServiceDTO requestMappingServiceDTO
     */
    void customHandle(RequestMappingServiceDTO requestMappingServiceDTO);

    /**
     * 获取需要鉴权的url
     * @author Victor.Xiao
     * @since 2023-10-17 16:56
     * @return java.util.Set<java.lang.String>
    */
    default Set<String> getPermissionUrls() {
        return PERMISSION_URLS;
    }
    /**
     * 获取需要所有的url
     * @author Victor.Xiao
     * @since 2023-10-17 16:56
     * @return java.util.Set<java.lang.String>
    */
    default Set<String> getAllUrls() {
        return ALL_URLS;
    }
}
